package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.mapper.TbProcessDepMapper;
import com.tju.resource.mapper.TbProcessGroupMapper;
import com.tju.resource.mapper.TbProdPlanMapper;
import com.tju.resource.model.TbProcessDep;
import com.tju.resource.model.TbProcessGroup;
import com.tju.resource.model.TbProdPlan;
import com.tju.resource.server.TbProcessGroupService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工艺路线管理实现接口
 */
@Slf4j
@Service
public class TbProcessGroupServiceImpl implements TbProcessGroupService {

    @Autowired
    private TbProcessGroupMapper tbProcessGroupMapper;
    @Autowired
    private TbProcessDepMapper tbProcessDepMapper;
    @Autowired
    private TbProdPlanMapper tbProdPlanMapper;
    @Autowired
    private ChineseExceptionUtils zh_CH;

    /**
     * 添加工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @Transactional
    @Override
    public Result<Object> addProcessGroup(TbProcessGroup tbProcessGroup) {
        try {
            if (tbProcessGroupMapper.insert(tbProcessGroup) > 0) {
                if (EmptyUtils.isNotEmpty(tbProcessGroup.getSeries())) {
                    for (int i = 0; i < tbProcessGroup.getSeries().length; i++) {
                        tbProcessDepMapper.insert(new TbProcessDep(Long.valueOf(tbProcessGroup.getSeries()[i]), tbProcessGroup.getId(), i));
                    }
                }
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.AddedSuccessfully);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            System.gc();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.AddFailed);
    }

    /**
     * 删除工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @Transactional
    @Override
    public Result<Object> deleteProcessGroup(TbProcessGroup tbProcessGroup) {
        try {
            if (EmptyUtils.isNotEmpty(tbProcessGroup.getId())) {
                QueryWrapper<TbProdPlan> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("group_number", tbProcessGroup.getId()).last("limit 1");
                if (EmptyUtils.isEmpty(tbProdPlanMapper.selectOne(queryWrapper))) {
                    if (tbProcessGroupMapper.deleteById(tbProcessGroup.getId()) > 0) {
                        QueryWrapper<TbProcessDep> wrapper = new QueryWrapper<>();
                        wrapper.eq("group_id", tbProcessGroup.getId());
                        tbProcessDepMapper.delete(wrapper);
                        return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyDeleted);
                    }
                } else
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.OperationGroupUsed);
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailedToDelete);
    }

    /**
     * 修改工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @Transactional
    @Override
    public Result<Object> modifyProcessGroup(TbProcessGroup tbProcessGroup) {
        try {
            if (EmptyUtils.isNotEmpty(tbProcessGroup.getId())) {
                if (tbProcessGroupMapper.updateById(tbProcessGroup) > 0) {
                    QueryWrapper<TbProcessDep> wrapper = new QueryWrapper<>();
                    wrapper.eq("group_id", tbProcessGroup.getId());
                    tbProcessDepMapper.delete(wrapper);
                    if (EmptyUtils.isNotEmpty(tbProcessGroup.getSeries())) {
                        for (int i = 0; i < tbProcessGroup.getSeries().length; i++) {
                            tbProcessDepMapper.insert(new TbProcessDep(Long.valueOf(tbProcessGroup.getSeries()[i]), tbProcessGroup.getId(), i));
                        }
                    }
                    return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
                }
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            System.gc();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailToEdit);
    }

    /**
     * 分页查询全部工艺流程管理信息
     *
     * @param parameter
     * @return
     */
    @Override
    public Result<Object> getProcessGroup(ClientParameters parameter) {
        try {
            if (EmptyUtils.isNotEmpty(parameter.getPage()) && EmptyUtils.isNotEmpty(parameter.getLimit())) {
                Page<TbProcessGroup> page = new Page<>(parameter.getPage(), parameter.getLimit());
                IPage<TbProcessGroup> iPage = tbProcessGroupMapper.getProcessGroup(page, parameter);
                return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getRecords().size());
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /*根据ID查询工艺信息*/
    @Override
    public TbProcessGroup getProcessGroupById(Long groupId) {
        return tbProcessGroupMapper.selectById(groupId);
    }
}
